package com.coolbi.onsupai.core.biz.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.coolbi.onsupai.core.biz.FranchiserFundBiz;
import com.coolbi.onsupai.core.constant.FranchiserFundConstant;
import com.coolbi.onsupai.core.dao.FranchiserFundDao;
import com.coolbi.onsupai.core.entity.FranchiserFund;
import com.coolbi.onsupai.core.exception.ServiceException;

public class FranchiserFundBizImpl implements FranchiserFundBiz{
	@Autowired
	private FranchiserFundDao franchiserFundDao;
	public void setFranchiserFundDao(FranchiserFundDao franchiserFundDao) {
		this.franchiserFundDao = franchiserFundDao;
	}

	@Override
	public FranchiserFund save(FranchiserFund obj) {
		try {
			franchiserFundDao.save(obj);
		} catch (Exception e) {
			throw new ServiceException("账户资金插入发生错误！",e);
		}
		return obj;
	}

	@Override
	public void delete(long id) {
		try {
			franchiserFundDao.delete(id);
		} catch (Exception e) {
			throw new ServiceException("资金账户删除发生错误！",e);
		}
	}

	@Override
	public void update(FranchiserFund obj) {
		try {
			FranchiserFund franchiserFund = franchiserFundDao.get(obj.getId());
			franchiserFund.setAccountType(obj.getAccountType());
			franchiserFund.setAccountNo(obj.getAccountNo());
			franchiserFund.setOwner(obj.getOwner());
			franchiserFund.setAccountStatus(obj.getAccountStatus());
			franchiserFund.setAccountType(obj.getAccountType());
			if (franchiserFundDao.update(franchiserFund) < 0) {
				throw new ServiceException("经销商资金账户修改发生失败！");
			}
		} catch (Exception e) {
			throw new ServiceException("经销商资金账户修改发生错误！",e);
		}
	}

	@Override
	public FranchiserFund get(long id) {
		return franchiserFundDao.get(id);
	}

	@Override
	public List<FranchiserFund> list() {
		return franchiserFundDao.list();
	}

	@Override
	public List<FranchiserFund> listByPage(int offset, int limit) {
		return franchiserFundDao.listByPage(offset, limit);
	}

	@Override
	public List<FranchiserFund> find(Map<String, Object> parameter) {
		return franchiserFundDao.find(parameter);
	}

	@Override
	public List<FranchiserFund> findByPage(Map<String, Object> parameter,
			int offset, int limit) {
		return franchiserFundDao.findByPage(parameter, offset, limit);
	}

	@Override
	public List<FranchiserFund> search(Map<String, Object> parameter, int offset,
			int limit) {
		return franchiserFundDao.search(parameter, offset, limit);
	}

	@Override
	public int count() {
		return franchiserFundDao.count();
	}

	@Override
	public int countByKeyWord(Map<String, Object> parameter) {
		return franchiserFundDao.countByKeyWord(parameter);
	}

	@Override
	public int count(Map<String, Object> parameter) {
		return franchiserFundDao.count(parameter);
	}

	@Override
	public boolean checkAccountNo(String accountNo) {
		return franchiserFundDao.countByAccountNo(accountNo) > 0;
	}

	@Override
	public void updateStatus(Long id, Boolean accountStatus) {
		try {
			if (franchiserFundDao.updateStatus(id, accountStatus) < 0) {
				throw new ServiceException("经销商资金账户状态修改发生失败！");
			}
		} catch (Exception e) {
			throw new ServiceException("经销商资金账户状态修改发生错误！",e);
		}
	}

	@Override
	public List<FranchiserFund> findByKeyWord(String searchType,
			String keyWord, int offset, int limit) {
		Map<String, Object> parameter = new HashMap<String, Object>();
		if ("1" == keyWord && "accountStatus" == searchType) {
			parameter.put(searchType, FranchiserFundConstant.ENABLE);
		} else if ("0" == keyWord && "accountStatus" == searchType) {
			parameter.put(searchType, FranchiserFundConstant.UNABLE);
		} else {
			parameter.put(searchType, keyWord);
		}
		return franchiserFundDao.search(parameter, offset, limit);
	}

	@Override
	public int countBykeyWord(String searchType, String keyWord) {
		Map<String, Object> parameter = new HashMap<String, Object>();
		if ("1" == keyWord && "accountStatus" == searchType) {
			parameter.put(searchType, FranchiserFundConstant.ENABLE);
		} else if ("0" == keyWord && "accountStatus" == searchType) {
			parameter.put(searchType, FranchiserFundConstant.UNABLE);
		} else {
			parameter.put(searchType, keyWord);
		}
		return franchiserFundDao.countByKeyWord(parameter);
	}

}
